<template>
    <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from '../../../assets/js/charts/resize'
export default {
    mixins: [resize],
    props: {
        chartData: {},
        className: {
            type: String,
            default: 'chart'
        },
        id: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100%'
        }
    },
    data() {
        return {
            chart: null,
        }
    },
    mounted() {
        this.initChart();
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(document.getElementById(this.id))
            // console.log(this.chartData)
            // 图表展示美化：人数的值小于5的时候，y轴坐标会显示1.5这样的值。这里设置人数最大小于10的时候，则设置y轴最大值为10；
            let maxVal =Math.max.apply(null, this.chartData.countData) <10 ? 10 : null; //y坐标最大值
            this.chart.setOption({
                title: {
                    text: this.chartData.timeTitle,
                    textStyle: {
                        fontWeight: 'normal',
                        fontSize: 14,
                        color: '#73D3FF'
                    },
                    x: 'center',
                    top: "5%",
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        lineStyle: {
                            color: '#08DBFF'
                        }
                    },
                    // formatter: '{b} <br /> {a0}: {c0}<br />{a1}: {c1} <br /> {a2}: {c2}' + "%"
                    formatter: '日期：{b} <br /> '+this.chartData.yAxisName+': {c0} <br />'
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '8%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#213967'
                        }
                    },
                    axisLabel: {
                        margin: 10,
                        textStyle: {
                            color: "#fff",
                            fontSize: 12
                        }
                    },
                    splitLine: {
                        show: true,
                        lineStyle: {
                            color: "#213967"
                        }
                    },
                    data: this.chartData.dateData
                }],
                yAxis: [{
                    type: 'value',
                    // name: '单位（%）',
                    axisTick: {
                        show: false
                    },
                    max:maxVal,
                    min:0,
                    axisLine: {
                        lineStyle: {
                            color: '#213967'
                        }
                    },
                    axisLabel: {
                        margin: 10,
                        textStyle: {
                            color: "#fff",
                            fontSize: 12
                        }
                    },
                    splitLine: {
                        show: true,
                        lineStyle: {
                            color: "#213967"
                        }
                    },
                }],
                series: [
                    {
                        // name: "折线图",
                        type: "line",
                        showSymbol: true,//显示折点
                        hoverAnimation: false,
                        symbolSize: 4,//折点的大小
                        lineStyle: {
                            color: "#4F92C4"//折线的颜色
                        },
                        areaStyle: {
                            color: {
                                type: "linear",
                                x: 0,
                                y: 0.9,
                                x2: 0,
                                y2: 0,
                                colorStops: [
                                    {
                                        offset: 0,
                                        color: 'rgba(44,76,146,.3)' // 0% 处的颜色
                                        // color: '#2C4C92' // 0% 处的颜色
                                    },
                                    {
                                        offset: 1,
                                        color: '#4681FF' // 100% 处的颜色
                                    }
                                ],
                                globalCoord: false // 缺省为 false
                            }
                        },
                        itemStyle: {
                            color: "#4681FF",
                            // borderWidth: 1,
                            borderColor: "#4681FF"
                        },
                        data: this.chartData.countData
                    },
                ]

            })
        }
    }
}
</script>
